package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Notice;
import com.powernode.model.Result;
import com.powernode.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 公告业务管理控制层
 */
@RestController
@Api(tags = "公告业务接口管理")
@RequestMapping("/shop/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;


    /**
     * 多条件分页查询公告列表
     *
     * @param current 当前页码
     * @param size    每页显示数量
     * @param title   标题
     * @param status  状态
     * @param isTop   是否置顶
     * @return
     */
    @ApiOperation("多条件分页查询公告列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('shop:notice:page')")
    public Result<Page<Notice>> loadNoticePage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) String title,
                                               @RequestParam(required = false) Integer status,
                                               @RequestParam(required = false) Integer isTop) {
        Page<Notice> noticePage = new Page<>(current, size);
        noticePage = noticeService.page(noticePage, new LambdaQueryWrapper<Notice>()
                .eq(ObjectUtil.isNotNull(status), Notice::getStatus, status)
                .eq(ObjectUtil.isNotNull(isTop), Notice::getIsTop, isTop)
                .like(StringUtils.hasText(title), Notice::getTitle, title)
                .orderByDesc(Notice::getCreateTime)
        );
        return Result.success(noticePage);
    }


    /**
     * 新增公告
     *
     * @param notice 新增的参数
     * @return
     */
    @ApiOperation("新增公告")
    @PostMapping
    @PreAuthorize("hasAuthority('shop:notice:save')")
    public Result<String> saveNotice(@RequestBody Notice notice) {
        boolean saved = noticeService.saveNotice(notice);
        return Result.handle(saved);
    }


    /**
     * 根据标识查询查询公告详情
     * @param noticeId 标识
     * @return
     */
    @ApiOperation("根据标识查询查询公告详情")
    @GetMapping("info/{noticeId}")
    @PreAuthorize("hasAuthority('shop:notice:info')")
    public Result<Notice> loadNoticeInfo(@PathVariable Long noticeId) {
        return Result.success(noticeService.getById(noticeId));
    }


    /**
     * 修改公告内容
     * @param notice 修改的参数
     * @return
     */
    @ApiOperation("修改公告内容")
    @PutMapping
    @PreAuthorize("hasAuthority('shop:notice:update')")
    public Result<String> modifyNotice(@RequestBody Notice notice) {
        boolean modified = noticeService.modifyNotice(notice);
        return Result.handle(modified);
    }


    /**
     * 根据标识删除公告
     * @param noticeId 删除的标识
     * @return
     */
    @ApiOperation("根据标识删除公告")
    @DeleteMapping("{noticeId}")
    @PreAuthorize("hasAuthority('shop:notice:update')")
    public Result<String> removeNotice(@PathVariable Long noticeId) {
        boolean removed = noticeService.removeById(noticeId);
        return Result.handle(removed);
    }
}
